package cat.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cat.entity.Article;
import cat.entity.Category;
import cat.mapper.ArticleCategoryRefMapper;
import cat.mapper.ArticleMapper;
import cat.service.ArticleService;

/**
 * 文章业务层实现类
 */
@Service
public class ArticleServiceImpl  implements ArticleService{
	@Resource
	ArticleMapper articleMapper;
	@Resource
	ArticleCategoryRefMapper articleCategoryRefMapper;
	/**
	 * 查询最新文章
	 */
	public List<Article> listRecentAritle(Integer n) {	
		return articleMapper.listRecentArticle(n);
	}

	
	/**
	 * 分页查询文章列表
	 */
	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
		PageHelper.startPage(pageIndex,pageSize);
		
		List<Article> articleList=articleMapper.findAll();
		
		//对每个文章,都把它对应的分类信息查出来
		for(Article a: articleList) {
			List<Category> categoryList=articleCategoryRefMapper.listCategoryByArticleId(a.getArticleId());
			a.setCategoryList(categoryList);
		}
	
		return new PageInfo<>(articleList);
	}
	
	@Override
	public void delArticle(Integer articleId) {
		articleMapper.delArticle(articleId);
	}
}